package com.oa.overtime.controller;

import java.io.OutputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.oa.entity.ExtPageable;
import com.oa.entity.ExtjsAjaxResult;
import com.oa.entity.WorkflowVariable;
import com.oa.log.annotation.Log;
import com.oa.overtime.dto.OvertimeDTO;
import com.oa.overtime.service.IOvertimeService;
import com.oa.util.SessionUtil;

@Controller
@RequestMapping("/overtime")
public class OvertimeController {

	@Resource
	private IOvertimeService overtimeService;

	/// overtime/findOvertimeForm
	@GetMapping(value = "/findOvertimeForm")
	public @ResponseBody Page<OvertimeDTO> findOvertimeForm(ExtPageable pageable) {
		return overtimeService.findAll(pageable.getPageable());
	}

	@GetMapping(value = "/findUserOvertime")
	public @ResponseBody Page<OvertimeDTO> findUserOvertime(OvertimeDTO dto, ExtPageable pageable) {
		return overtimeService.findOvertimeForm(OvertimeDTO.getOvertimeWhereClause(dto), pageable.getPageable());
	}

	@GetMapping(value = "/findUserOvertimeForm")
	public @ResponseBody Page<OvertimeDTO> findOvertimeForm(OvertimeDTO dto, ExtPageable pageable) {
		return overtimeService.findOvertimeForm(OvertimeDTO.getWhereClause(dto), pageable.getPageable());
	}

	@PostMapping("/saveOrUpdateForm")
	@Log(oprate = "保存或更新加班草稿")
	public @ResponseBody ExtjsAjaxResult saveOrUpdateForm(OvertimeDTO overtimeDTO) {
		try {
			overtimeDTO.setCreateDate(new Date());
			overtimeDTO.setState("草稿");
			overtimeService.save(overtimeDTO);
			return new ExtjsAjaxResult(true, "操作成功");
		} catch (Exception e) {
			return new ExtjsAjaxResult(false, "操作失败");
		}
	}

	/// overtime/delete
	@PostMapping("/delete")
	@Log(oprate = "删除加班申请")
	public @ResponseBody ExtjsAjaxResult delete(Long id) {
		try {
			overtimeService.delete(id);
			return new ExtjsAjaxResult(true, "删除成功");
		} catch (Exception e) {
			return new ExtjsAjaxResult(false, "删除失败");
		}
	}

	/// overtime/starOvertimeFlow
	@PostMapping("/starOvertimeFlow")
	@Log(oprate = "提交加班申请审批")
	public @ResponseBody ExtjsAjaxResult starOvertimeFlow(Long overtimeId, String processInstanceId,
			HttpSession session) {
		if (overtimeService.isProcessInstanceActive(processInstanceId)) {
			return new ExtjsAjaxResult(false, "该申请正在审批过程中");
		}
		Map<String, Object> variabels = new HashMap<>();
		try {

			variabels.put("applyUserId", SessionUtil.getUserId(session));
			variabels.put("depManager", overtimeService.getDeptManager(SessionUtil.getUserInfo(session).getDept()));

			overtimeService.starProcess(overtimeId, variabels);
			return new ExtjsAjaxResult(true, "提交成功");
		} catch (Exception e) {
			e.printStackTrace();
			return new ExtjsAjaxResult(false, "提交失败");
		}
	}

	/// overtime/findTodeTask
	@GetMapping("/findTodeTask")
	public @ResponseBody Page<OvertimeDTO> findTodeTask(HttpSession session, ExtPageable pageable) {
		return overtimeService.findTodoTask(SessionUtil.getUserId(session), pageable.getPageable());
	}

	/// overtime/overtimeClime
	@RequestMapping("/overtimeClime")
	@Log(oprate = "签收加班申请")
	public @ResponseBody ExtjsAjaxResult overtimeClime(String taskId, HttpSession session) {
		try {
			overtimeService.claim(taskId, SessionUtil.getUserId(session));
			return new ExtjsAjaxResult(true, "签收成功");
		} catch (Exception e) {
			e.printStackTrace();
			return new ExtjsAjaxResult(false, "签收失败");
		}
	}

	/// overtime/overtimeComplete
	@RequestMapping("/overtimeComplete")
	@Log(oprate = "审批加班申请")
	public @ResponseBody ExtjsAjaxResult overtimeComplete(OvertimeDTO dto, WorkflowVariable variable,
			HttpSession session) {
		try {
			overtimeService.complete(dto, variable.getVariableMap());
			return new ExtjsAjaxResult(true, "操作成功");
		} catch (Exception e) {
			e.printStackTrace();
			return new ExtjsAjaxResult(false, "操作失败");
		}
	}

	/// overtime/findToFileForm
	@RequestMapping("/findToFileForm")
	public @ResponseBody Page<OvertimeDTO> findToFileForm(HttpSession session, ExtPageable pageable) {
		return overtimeService.findToFileForm(SessionUtil.getUserId(session), pageable.getPageable());
	}

	/// overtime/file
	@RequestMapping("/file")
	@Log(oprate = "归档加班申请")
	public @ResponseBody ExtjsAjaxResult file(OvertimeDTO dto) {
		try {
			overtimeService.overtimeFile(dto);
			return new ExtjsAjaxResult(true, "操作成功");
		} catch (Exception e) {
			e.printStackTrace();
			return new ExtjsAjaxResult(false, "操作失败");
		}
	}

	/// overtime/isProcessActive
	@RequestMapping("/isChangeable")
	public @ResponseBody ExtjsAjaxResult isChangeable(String processInstanceId) {
		if (overtimeService.isProcessInstanceActive(processInstanceId)) {
			return new ExtjsAjaxResult(false, "该申请正在进行中");
		}
		if (overtimeService.isProcessInstanceExist(processInstanceId)) {
			return new ExtjsAjaxResult(false, "该表单已提交过");
		}
		return new ExtjsAjaxResult(true, "该申请正没有在进行");
	}

	@GetMapping("/findLateForm")
	public @ResponseBody Page<OvertimeDTO> findLateForm() {
		return overtimeService.findLateForm();
	}

	@GetMapping("/getLeader")
	public @ResponseBody String getLeader(HttpSession session) {
		return overtimeService.getDeptManager(SessionUtil.getUserInfo(session).getDept());
	}

	@RequestMapping("/export")
	public @ResponseBody void export(HttpServletRequest request, HttpServletResponse response, OvertimeDTO dto) {
		try {
			HSSFWorkbook wb = overtimeService.export(OvertimeDTO.getWhereClause(dto));
			response.setContentType("application/vnd.mx-excel");
			response.setHeader("Content-disposition", "attachment;filename=overtime.xls");

			OutputStream outputStream = response.getOutputStream();
			wb.write(outputStream);
			outputStream.flush();
			outputStream.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
